/**
 *	Contains the validator object and code
**/

"use strict"

if (typeof GameLogic === "undefined") {
	var GameLogic = { }
}

GameLogic.validator = {	

	//validator main function
	validate: function(game, move) {
		this.game = game
		this.move = move
		
		var validators = [									//run the validators in this order!
			"_checkGameAlreadyWon",
			"_checkSquareAlreadyFilled"
		]
		var result
		
		for (var i=0, ii=validators.length; i<ii; i++) {	//loop through victory contitions looking for winner
			result = this[ validators[i] ]()
			//if (result == false)  return false
		}
		return true											//must be a valid move at this point
	},
	
	//validator functions

	_checkGameAlreadyWon: function() {
		if (this.game.winner !== 0) {
			var winner = ( this.game.winner === -1 ? "draw" : this.game.players[ this.game.winner-1 ] )
			throw "game already won by: " + winner
		}
		return true
	},

	_checkSquareAlreadyFilled: function() {
		var y = this.move.y, x = this.move.x
		if (this.game.state[y][x] !== 0) {
			throw "square already occupied"
		}
		return true
	}
	
}
